package stu.day33_day38.day34;

import java.util.Scanner;

public class hw3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int target  = Integer.parseInt(sc.nextLine());
        String[] str = sc.nextLine().split(" ");


        int[] arr = new int[str.length];
        for(int i=0;i<str.length;i++){
            arr[i] = Integer.parseInt(str[i]);
        }


        int left = 0;
        int sum = 0;
        int max = 0;
        for(int right=0;right<arr.length;right++){
            sum += arr[right];
            while (sum>target){
                sum -= arr[left];
                left += 1;
            }
            max = Math.max(max,sum);

        }
        System.out.println();
        System.out.println(max);
    }
}
